<template>
  <div class="file-operation-page">
    <el-button type="primary" @click="exportToImage">展示</el-button>
    <div class="ecahrts-xrq">
      <h1>内容渲染区</h1>
      <div class="block" ref="exportContent">
        <el-timeline>
          <el-timeline-item timestamp="2018/4/12" placement="top">
            <el-card>
              <h4>更新 Github 模板</h4>
              <p>王小虎 提交于 2018/4/12 20:46</p>
            </el-card>
          </el-timeline-item>
          <el-timeline-item timestamp="2018/4/3" placement="top">
            <el-card>
              <h4>更新 Github 模板</h4>
              <p>王小虎 提交于 2018/4/3 20:46</p>
            </el-card>
          </el-timeline-item>
          <el-timeline-item timestamp="2018/4/2" placement="top">
            <el-card>
              <h4>更新 Github 模板</h4>
              <p>王小虎 提交于 2018/4/2 20:46</p>
            </el-card>
          </el-timeline-item>
          <el-timeline-item timestamp="2018/4/1" placement="top">
            <el-card>
              <h4>更新 Github 模板</h4>
              <p>王小虎 提交于 2018/4/1 20:46</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
    <div class="ecahrts-zsq">
      <h1>图片展示区</h1>
      <img v-if="echartsUrl" :src="echartsUrl" alt="" />
    </div>
  </div>
</template>

<script setup>
import html2canvas from "html2canvas";
const exportContent = ref(null);
const echartsUrl = ref(null)
const exportToImage = async () => {
  if (exportContent.value) {
    try {
      // 使用 html2canvas 渲染指定的 DOM 元素
      const canvas = await html2canvas(exportContent.value, {
        backgroundColor: 'black', // 保留背景透明度
        useCORS: true, // 启用跨域资源支持
      });

      // 将画布转换为图片数据 URL
      const imageData = canvas.toDataURL("image/png");
      echartsUrl.value = imageData
    } catch (error) {
      console.error("导出图片失败:", error);
    }
  }
};
onMounted(() => {
  exportToImage()
})
</script>

<style lang="scss" scoped>
.file-operation-page {
  padding: 10px 15px;
  width: 100%;
  height: 100%;
  display: flex;
  position: relative;

  >button {
    position: absolute;
    right: 0;
    top: 0;
  }

  >div {
    h1 {
      margin-bottom: 20px;
    }
  }

  .ecahrts-xrq {
    width: 900px;
    margin-right: 20px;

    .block {
      width: 100%;
      height: 550px;

      :deep(.el-timeline) {
        width: 100%;
        height: 100%;

        .el-card {
          background-color: transparent;

          .el-card__body {
            width: 100%;
            background-color: black;
          }
        }
      }
    }
  }

  .ecahrts-zsq {
    width: calc(100% - 920px);

    img {
      width: 450px;
      height: 275px;
    }
  }

  .file-operation {
    width: 100%;
    height: 100px;
  }

  .file-view {
    width: 100%;
    height: calc(100% - 100px);
  }
}
</style>